<?php
/**
 * Created by PhpStorm.
 * User: janke
 * Date: 17-3-19
 * Time: 下午9:45
 */

namespace app\controllers;

use app\models\Message;
use app\models\User;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\QueryParamAuth;
use yii\web\Response;
use yii\rest\ActiveController;


class MessageController extends ActiveController
{
    public $modelClass = 'app\models\Message';
    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    /**
     * 提前操作
     * @param \yii\base\Action $action
     * @return bool
     * @author Janke
     */
    public function beforeAction($action)
    {
        User::$accessToken = \Yii::$app->request->get()['access-token'];
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    /**
     * API动作限制
     * @author janke
     * define the allowed actions
     */
    public function actions()
    {
        $actions =  parent::actions(); // TODO: Change the autogenerated stub
        unset($actions['options'], $actions['update'], $actions['delete']);
        return $actions;
    }

    /**
     * POST 插入一条message
     * @POST messageSender
     * @POST messageContent
     * @POST messagePicUrl (NULL)
     * @POST isFake
     * @return array | Boolean
     */
    public function actionInsert()
    {
        $postData = \Yii::$app->request->post();
        $messageModel = new Message();
        $messageModel->setAttributes($postData);
        if ($messageModel->validate())
        {
            return $this->makeReturn($messageModel->insertMessage());
        }
        return $messageModel->errors;
    }

    /**
     * 删除 message
     * @POST messageId
     * @return array
     * @author Janke
     */
    public function actionDelete_message()
    {
        $postData = \Yii::$app->request->post();
        $messageModel = new Message();

        if ($postData['messageId'])
        {
            $deleteResult = $messageModel->deleteMessageById($postData['messageId']);
            if ($deleteResult)
                return $this->makeReturn($deleteResult);
        }else{
            $messageModel->addError("messageId", "messageId is required!");
        }
        return $messageModel->errors;
    }

    /**
     * 对操作结果作出反馈
     * @param $result
     * @param array $paramAry
     * @return array
     */
    private function makeReturn($result=0, $paramAry = [])
    {
        $ret = [
            'isSuccess' => $result,
        ];
        return array_merge($ret,$paramAry);
    }

    public function actionApprove()
    {

    }

    public function actionDisapprove()
    {

    }


    /**
     * api设置验证，输出格式化
     * @author janke
     * @return array
     */
    public function behaviors()
    {
        // set response type
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        // authorization
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                QueryParamAuth::className(),
            ]
        ];
        return $behaviors;
    }
}